package com.sdkx.app.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.AirWayManager;
import com.sdkx.app.model.AirWay;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.util.Util;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class AirWayDetailFragment extends BaseFragment {
    RecyclerView recyclerView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.airway_detail_layout,null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        recyclerView = (RecyclerView) view.findViewById(R.id.relistview);
        view.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        AirWayDetailAdapter airWayDetailAdapter = new AirWayDetailAdapter();
        Bundle bundle = getArguments();
        String wayname = bundle.getString("AIRWAY_NAME");
        if(!TextUtils.isEmpty(wayname)) {
            airWayDetailAdapter.setWayPoints(AirWayManager.getWayPointsInAirWay(wayname));
            LinearLayoutManager linearLayoutManager =  new LinearLayoutManager(getContext());
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(airWayDetailAdapter);
            airWayDetailAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    class ViewHodler extends RecyclerView.ViewHolder{
        TextView name,lat,lon,direction,time;
        public ViewHodler(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.wpname);
            lat = itemView.findViewById(R.id.wp_lat);
            lon = itemView.findViewById(R.id.wp_lon);
            direction = itemView.findViewById(R.id.direction);
            time = itemView.findViewById(R.id.wp_time);
        }
        public void init(WayPoint point){
            itemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            name.setText(point.getName());
            lat.setText(Util.degreePatten(getContext(),point.getLatitude()));
            lon.setText(Util.degreePatten(getContext(),point.getLongitude()));
            direction.setText("0.0");
            time.setText("--:--:--");
        }
    }
    private class AirWayDetailAdapter extends RecyclerView.Adapter<ViewHodler>{
         List<WayPoint> wayPoints = new ArrayList<>();
         public void setWayPoints(List<WayPoint> list){
             wayPoints = list;
         }

        @NonNull
        @Override
        public ViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHodler(getLayoutInflater().inflate(R.layout.airway_detail_item,null));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHodler holder, int position) {
            holder.init(wayPoints.get(position));
        }

        @Override
        public int getItemCount() {
            return wayPoints.size();
        }
    }
}
